package com.sgb.kjwl.view.ui.adapter.self.packers;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.self.SelfTeamAlbumEntity;

import java.util.ArrayList;
import java.util.List;

public class SelfTeamAlbumEditAdapter extends RecyclerView.Adapter<SelfTeamAlbumEditAdapter.EditViewHolder> {

    private Context mContext;
    private List<SelfTeamAlbumEntity> urls;
    private List<SelfTeamAlbumEntity> selectedUrls = new ArrayList<>();
    private SelectedListener selectedListener;
    private RequestManager requestManager;
    private GridLayoutManager mGridLayoutManager;

    public SelfTeamAlbumEditAdapter(Context context, List<SelfTeamAlbumEntity> list, RequestManager requestManager, GridLayoutManager manager) {
        this.mContext = context;
        this.urls = list;
        this.requestManager = requestManager;
        this.mGridLayoutManager = manager;
    }

    @NonNull
    @Override
    public EditViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_self_team_album, viewGroup, false);
        return new EditViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull EditViewHolder editViewHolder, int i) {
        RequestOptions options = new RequestOptions();
        options.override(400, 400);
        requestManager.load(urls.get(i).getUrl()).apply(options).into(editViewHolder.img);
        editViewHolder.delView.setVisibility(View.VISIBLE);

        if (selectedUrls.contains(urls.get(i))) {
            editViewHolder.delView.setBackgroundResource(R.mipmap.selected_website);
        } else {
            editViewHolder.delView.setBackgroundResource(R.mipmap.unselected_website);
        }

        editViewHolder.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (selectedUrls.contains(urls.get(i))) {
                    selectedUrls.remove(urls.get(i));
                } else {
                    if (selectedUrls.size() < 1) {
                        selectedUrls.add(urls.get(i));
                    } else {
                        Toast.makeText(mContext, "每次只能删除一张", Toast.LENGTH_SHORT).show();
                        editViewHolder.delView.setBackgroundResource(R.mipmap.unselected_website);
                    }
                }
                notifyDataSetChanged();
                if (null != selectedListener) {
                    selectedListener.setData(selectedUrls);
                }
            }
        });
        ViewGroup.LayoutParams parm = editViewHolder.itemView.getLayoutParams();
        parm.height =
                mGridLayoutManager.getWidth() / mGridLayoutManager.getSpanCount()
                        - 2 * editViewHolder.itemView.getPaddingLeft() - 2 * ((ViewGroup.MarginLayoutParams) parm).leftMargin;
    }

    @Override
    public int getItemCount() {
        return urls == null ? 0 : urls.size();
    }

    /**
     * 正常内容的viewholder
     */
    class EditViewHolder extends RecyclerView.ViewHolder {
        ImageView img;
        ImageView delView;

        EditViewHolder(View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.photo_img);
            delView = itemView.findViewById(R.id.select_iv);
        }
    }

    public interface SelectedListener {
        void setData(List<SelfTeamAlbumEntity> urls);
    }

    public void setSelectListener(SelectedListener listener) {
        this.selectedListener = listener;
    }

    public void setSelectAll(boolean flag) {
        if (flag) {
            selectedUrls.clear();
            selectedUrls.addAll(urls);
        } else {
            selectedUrls.clear();
        }
        notifyDataSetChanged();
    }

    public List<SelfTeamAlbumEntity> getSelectedUrls() {
        return selectedUrls;
    }
}
